//
//  TaskView.m
//  Civacrm
//
//  Created by 王桂敏 on 2017/2/24.
//  Copyright © 2017年 王桂敏. All rights reserved.
//

#import "TaskView.h"
#import "CourseViewController.h"
#import "TabModel.h"
#import "CourseDetailViewController.h"

@implementation TaskView

- (UIView *)tabView{
    if (!_tabView) {
        self.tabView = [UIView viewWithFrame:CGRectMake(0, 0, kScreenWidth, h(62)) bgColor:WhiteColor];
        
        self.tabCollectView = [[CollectionView alloc] initViewWithType:CollectionViewType1 frame:CGRectMake(0, 0, kScreenWidth, h(42))];
        [_tabView addSubview:self.tabCollectView];
        //  更多按钮
        self.moreButton = [UIButton buttonOfImageForNormalWithImage:@"icon_gengduo" frame:CGRectMake(kScreenWidth - w(10) - w(62), 0, w(62), h(42)) target:self action:@selector(handleMoreButtonAction)];
        self.moreButton.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
        [self.tabCollectView addSubview:self.moreButton];
        //  临时修改样式按钮
        self.changeButton = [UIButton buttonOfImageForNormalWithImage:@"icon_qiehuan" frame:CGRectMake(self.moreButton.x - w(62) - w(20), 0, w(62), h(42)) target:self action:@selector(handleChangeButtonAction)];
        self.changeButton.contentVerticalAlignment = UIControlContentVerticalAlignmentBottom;
//        [self.tabCollectView addSubview:self.changeButton];
    }
    return _tabView;
}

- (CourseCollectionView *)collectionView{
    if (!_collectionView) {
        self.collectionView = [[CourseCollectionView alloc] initWithFrame:CGRectMake(w(20), 0, kScreenWidth - w(40), h(422))];
        _collectionView.delegate = self;
        [_collectionView setViewWithType:CourseCollectionViewType0
                                     col:2];
    }
    return _collectionView;
}

- (void)configCollectViewWithRow:(NSInteger)row
                             col:(NSInteger)col
                      dataSource:(NSMutableArray *)dataSource{
    
    self.collectionView.height = (dataSource.count / col + (dataSource.count%col ? 1 : 0)) * h(211);
    [self.collectionView setViewWithDataSource:dataSource col:col];
    
}
- (void)configTabViewWithTitle:(NSString *)title{
    TabModel *model = [[TabModel alloc] init];
    model.name = title;
    [self.tabCollectView setDataSource:[NSMutableArray arrayWithObject:model]];
}

#pragma mark - buttonAction
- (void)handleMoreButtonAction{
    CourseViewController *VC = [[CourseViewController alloc] init];
    [self.tabView.viewController.navigationController pushViewController:VC animated:YES];
}

- (void)handleChangeButtonAction{
    [self.collectionView setViewWithType:0
                                     col:2 + (1 - (self.collectionView.col - 2))];
}
#pragma mark - CourseCollectionViewDelegate
- (void)collectionView:(CourseCollectionView *)collectionView didSelect:(NSInteger)index type:(CourseCollectionViewType)type{
    CourseDetailViewController *VC = [[CourseDetailViewController alloc] init];
    VC.webURL = [NSString stringWithFormat:@"%@&uId=%@&taskId=%@",H5url,self.userID,[[self.collectionView.dataSource[index] taskModel] taskId]];
    VC.model = self.collectionView.dataSource[index];
    [self.collectionView.viewController.navigationController pushViewController:VC animated:YES];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
